How to detect if a button has been pressed in a simple way
There are two ways to detect if a button has been pressedβ
First wayβ
- Just add a SUIKeyEventListener to the button in the interface in the properties panel, define its Key and then check it through a script.
In your Java class, do the following:β
package JAVARuntime;
public class YourClass extends Component {
@Override
public void start() {
}
@Override
public void repeat() {
if(Input.getKey("yourKey").isDown()) {
// your code
}
}
}
Second wayβ
- Just create a public variable of type SUIButton and select your button on the interface in the properties panel.
β οΈ There are many ways to search for a component, to better understand this see the topic Search components, you can search your button in any of these ways. β οΈ
In your Java class, do the following:β
package JAVARuntime;
public class YourClass extends Component {
public SUIButton myButton;
@Override
public void start() {
}
@Override
public void repeat() {
if(myButton.isDown()) {
// your code
}
}
}
Understanding the codeβ
isDown()β
- Detect touch when the button is touched.
isUp()β
- Detects touch when the button is released.
isPressed()β
- Detects touch while the button is being pressed.